<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View, Session};
use think\exception\ValidateException;

use app\admin\validate\ShopValidate;

use app\admin\model\Admin;

class ShopController extends BaseController
{
    private $adminModel;
    private $token;
    public function __construct(Admin $adminModel)
    {
        $this->adminModel = $adminModel;
        $this->token = Session::get('token');
    }

    // 店主列表页面
    public function shop_list()
    {   
        return view();
    }

    // 渲染店主编辑页面
    public function shop_edit()
    {   
        $data = input('get.');
        $adminModel = new Admin();

        $adminInfo = $adminModel->getFind(['id' => $data['id']])['data'];
        $regionArr = explode('-', $adminInfo['region']);

        $adminInfo['province'] = $regionArr[0];
        $adminInfo['city']     = $regionArr[1];
        $adminInfo['area']     = $regionArr[2];

        $admin_list = $adminModel->getList([['role_id', '=', $adminInfo->getData('role_id') - 1]], 0);

        View::assign('admin_list', $admin_list['data']);
        View::assign('admin', $adminInfo);
        return view();
    }

    // 获取店主列表
    public function shop_data()
    {
        $data = input('get.');

        // 验证数据
        try{
            validate(ShopValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where[] = ['del_time', '=', 0];
        $where[] = ['role_id', '>', 1];

        if($this->token['role_id'] == 1){
            $where[] = ['pid', '=', $this->token['id']];
        }

        // 搜索条件
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname'])){
                $where[] = ['nickname', 'like', '%' . $data['key']['nickname'] . '%'];
            }
        }

        $adminModel = new Admin();

        $count = $adminModel->getNum($where)['data'];

        $field = 'id, pid, nickname, money, money_gold, point_radio, concat(region, "-" ,address) as address, is_business, role_id, status, create_time';
        $list = $adminModel->getList($where, 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            $v['pid_name'] = $adminModel->getVal(['id' => $v['pid']], 'nickname')['data'];
        }

        $this->success_back('ok', $list['data'], $count);
    }

    // 店主的启用||禁用
    public function setStatus()
    {
        $id = input('post.id');
        $adminModel = new Admin();
        $status = $adminModel->setStatus($id);

        $this->success_back($status['msg']);
    }

    // 店主信息修改
    public function shop_edit_data()
    {   
        $adminModel = new Admin();
        $data = input('post.');
        $data['logo'] = request()->file('logo');

        if($data['logo']){
            $field = 'edit_img';
        }else{
            $field = 'edit';
        }

        // 验证数据
        try{
            validate(ShopValidate::class)->scene($field)->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $username_num = $adminModel->getNum(['username' => $data['username']]);

        if($username_num['data'] > 1){
            $this->error_back('该账号已存在');
        }

        if(empty($data['password'])){
            unset($data['password']);
        }else{
            $data['password'] = setPass($data['password']);
        }

        if($data['logo']){
            $data['logo'] = fileUp($data['logo'], 'logo');
        }else{
            unset($data['logo']);
        }
        
        $data['region'] = $data['province'] . '-' . $data['city'] . '-' . $data['area'];
        $flag = $adminModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        if($this->token['role_id'] == 2){
            $show = 0;
        }else{
            $show = 1;
        }

        $this->success_back('修改成功', $show);
    }

    // 删除店主
    public function shop_del()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(ShopValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $adminModel = new Admin();

        $flag = $adminModel->edit(['id' => $data['id'], 'del_time' => time()]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
